package com.mybatis.exercise.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mybatis.exercise.core.base.BaseService;
import com.mybatis.exercise.core.common.helper.DaoHelper;
import com.mybatis.exercise.model.pojo.User;
import com.mybatis.exercise.model.vo.UserVo;
import com.mybatis.exercise.service.UserService;

@Transactional
@Service
public class UserServiceImpl extends BaseService implements UserService {
	//声明日志对象 logback 
	private final static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	private final static String nameSpace = "com.mybatis.exercise.mapper.UserMapper";
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean deleteUser(Integer userId) {
		logger.trace("trace");
		logger.debug("debug str");
		logger.info("info str");
		logger.warn("warn");
		logger.error("error");
		return DaoHelper.delete(nameSpace, "deleteUser",userId);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public Object insertUser(User user) {
		return (Object)DaoHelper.insert(nameSpace, "insertUser", user);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public UserVo selectUserByUserId(Integer userId) {
		return (UserVo)DaoHelper.query(nameSpace, "selectUserByUserId", userId);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<UserVo> selectUsers() {
		return (List<UserVo>)DaoHelper.list(nameSpace, "selectUsers", null);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public UserVo selectUserByEmailAndPwd(String userEmail, String userPassword) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userEmail", userEmail);
		map.put("userPassword", userPassword);
		return (UserVo)DaoHelper.query(nameSpace, "selectUserByUserEmailAndPwd", map);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean updateUser(User user) {
		return DaoHelper.update(nameSpace, "updateUser", user);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<UserVo> selectUsersByCondition(Map<String, Object> conditions) {
		
		return (List<UserVo>)DaoHelper.list(nameSpace, "selectUsersByCondition", conditions);
	}

}
